package com.ph.irp.base.incomeexpense.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.accountbook.dao.PBaseAccountbookDAO;
import com.ph.irp.base.incomeexpense.dao.IncomeExpenseDAO;
import com.ph.irp.base.incomeexpense.service.IncomeExpenseService;
import com.ph.irp.dto.IncomeExpenseDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.irp.model.PBaseIncomeexpense;

public class IncomeExpenseServiceImpl implements IncomeExpenseService {
	IncomeExpenseDAO incomeExpenseDaoProxy;
	PBaseAccountbookDAO accountBookDaoProxy;

	public void setIncomeExpenseDaoProxy(IncomeExpenseDAO incomeExpenseDaoProxy) {
		this.incomeExpenseDaoProxy = incomeExpenseDaoProxy;
	}

	public void setAccountBookDaoProxy(PBaseAccountbookDAO accountBookDaoProxy) {
		this.accountBookDaoProxy = accountBookDaoProxy;
	}

	public List<IncomeExpenseDTO> getAllIncomeExpense() throws Throwable {
		List<IncomeExpenseDTO> IncomeExpenseDTOList = new ArrayList<IncomeExpenseDTO>();
		List<PBaseIncomeexpense> IncomeexpenseList = incomeExpenseDaoProxy
				.getAllIncomeExpense();

		for (PBaseIncomeexpense ie : IncomeexpenseList) {
			IncomeExpenseDTO ieDTO = new IncomeExpenseDTO();
			ieDTO.setIncomeExpenseCode(ie.getIncomeExpenseCode());
			ieDTO.setIncomeExpenseDesc(ie.getIncomeExpenseDesc());
			ieDTO.setIncomeExpenseId(String.valueOf(ie.getIncomeExpenseId()));
			ieDTO.setIncomeExpenseName(ie.getIncomeExpenseName());
			ieDTO.setIncomeExpenseType(ie.getIncomeExpenseType() == 0 ? "收入"
					: "支出");
			ieDTO.setAccountBookName(accountBookDaoProxy.getAccountbookById(
					ie.getPBaseAccountbookId()).getAccountbookName());
			ieDTO.setForbidden(ie.getForbidden() == 0 ? "使用" : "禁用");
			IncomeExpenseDTOList.add(ieDTO);
		}
		return IncomeExpenseDTOList;
	}

	public List<IncomeExpenseDTO> getIncomeExpenseByNameAndCode(String code,
			String name) throws Throwable {
		List<IncomeExpenseDTO> IncomeExpenseDTOList = new ArrayList<IncomeExpenseDTO>();
		List<PBaseIncomeexpense> IncomeexpenseList = incomeExpenseDaoProxy
				.getIncomeExpenseByNameAndCode(code, name);

		for (PBaseIncomeexpense ie : IncomeexpenseList) {
			IncomeExpenseDTO ieDTO = new IncomeExpenseDTO();
			ieDTO.setIncomeExpenseCode(ie.getIncomeExpenseCode());
			ieDTO.setIncomeExpenseDesc(ie.getIncomeExpenseDesc());
			ieDTO.setIncomeExpenseId(String.valueOf(ie.getIncomeExpenseId()));
			ieDTO.setIncomeExpenseName(ie.getIncomeExpenseName());
			ieDTO.setIncomeExpenseType(ie.getIncomeExpenseType() == 0 ? "收入"
					: "支出");
			ieDTO.setAccountBookName(accountBookDaoProxy.getAccountbookById(
					ie.getPBaseAccountbookId()).getAccountbookName());
			ieDTO.setForbidden(ie.getForbidden() == 0 ? "使用" : "禁用");
			IncomeExpenseDTOList.add(ieDTO);
		}
		return IncomeExpenseDTOList;
	}

	public void addIncomeExpense(IncomeExpenseDTO ieDTO) throws Throwable {
		PBaseIncomeexpense ie = new PBaseIncomeexpense();
		ie.setIncomeExpenseCode(ieDTO.getIncomeExpenseCode());
		ie.setIncomeExpenseDesc(ieDTO.getIncomeExpenseDesc());
		ie.setIncomeExpenseName(ieDTO.getIncomeExpenseName());
		ie.setIncomeExpenseType(Integer.parseInt(ieDTO.getIncomeExpenseType()));
		ie.setPBaseAccountbookId(Integer.parseInt(ieDTO.getAccountbookId()));

		incomeExpenseDaoProxy.addIncomeExpense(ie);
	}

	public IncomeExpenseDTO getIncomeExpenseByIncomeExpenseId(String expenseID)
			throws Throwable {
		IncomeExpenseDTO ieDTO = new IncomeExpenseDTO();
		PBaseIncomeexpense ie = incomeExpenseDaoProxy
				.getIncomeExpenseByIncomeExpenseId(expenseID);

		ieDTO.setIncomeExpenseCode(ie.getIncomeExpenseCode());
		ieDTO.setIncomeExpenseDesc(ie.getIncomeExpenseDesc());
		ieDTO.setIncomeExpenseId(String.valueOf(ie.getIncomeExpenseId()));
		ieDTO.setIncomeExpenseName(ie.getIncomeExpenseName());
		ieDTO.setIncomeExpenseType(String.valueOf(ie.getIncomeExpenseType()));
		PBaseAccountbook acountBook = accountBookDaoProxy.getAccountbookById(ie
				.getPBaseAccountbookId());
		if (acountBook != null) {
			ieDTO.setAccountBookName(acountBook.getAccountbookName());
		}
		ieDTO.setForbidden(ie.getForbidden() == 0 ? "使用" : "禁用");

		return ieDTO;

	}

	public void editIncomeExpense(IncomeExpenseDTO editedIncomeExpense)
			throws Throwable {
		PBaseIncomeexpense ie = new PBaseIncomeexpense();
		ie.setIncomeExpenseCode(editedIncomeExpense.getIncomeExpenseCode());
		ie.setIncomeExpenseDesc(editedIncomeExpense.getIncomeExpenseDesc());
		ie.setIncomeExpenseId(Integer.parseInt(editedIncomeExpense
				.getIncomeExpenseId()));
		ie.setIncomeExpenseName(editedIncomeExpense.getIncomeExpenseName());
		ie.setIncomeExpenseType(Integer.parseInt(editedIncomeExpense
				.getIncomeExpenseType()));
		ie.setPBaseAccountbookId(Integer.parseInt(editedIncomeExpense.getAccountbookId()));
		
		incomeExpenseDaoProxy.editIncomeExpense(ie);
	}

	public void deleteIncomeExpense(String deleteIds) throws Throwable {
		incomeExpenseDaoProxy.deleteIncomeExpense(deleteIds);
	}

	public void forbidIncomeExpense(String forbidIds) throws Throwable {
		String[] forbidIdArray = forbidIds.split(",");
		for (String forbidId : forbidIdArray) {
			incomeExpenseDaoProxy.forbidIncomeExpense(forbidId);
		}

	}

	public void antiForbidIncomeExpense(String forbidIds) throws Throwable {
		String[] forbidIdArray = forbidIds.split(",");
		for (String forbidId : forbidIdArray) {
			incomeExpenseDaoProxy.antiForbidIncomeExpense(forbidId);
		}
	}
}
